//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created   : 08/05/2005
// Last Modif: 09/05/2005
//


var CLAR_HINTS=[];
var CLAR_OBJS=[];


function rebuild_clars() {
  CLAR_HINTS=getMenuC();
  CLAR_OBJS=getMenuGC();
}

rebuild_clars();


function clickclar(cl) {
  if(mainform.C[cl].checked) {
    SetClaimUnclear(cl);
  } else {
    SetClaimClear(cl);
  }
}

function is_clarhint(txt) {
  for(var e in CLAR_HINTS) {
    if(CLAR_HINTS[e] && CLAR_HINTS[e]==txt) {
      return true;
    }
  }
  return false;
}

function gclargetallobjs(cl,callback) {
  var objs_iter= new claimclarity_iterator(cl);
  var html="";
  while(!objs_iter.end()) {
    html+="<input type=button class=GCLARO value='"+objs_iter.getType()+"("+objs_iter.getParam()+")'>";
    html+="<button class=GCLAR onclick='gclarselectionrem("+cl+",\""+(callback||"")+"\")'>"+html_img(smallmin_img)+"</button> ";
    objs_iter.next();
  }
  return html;
}

function gclarselectionadd(cl,callback) {
  AddClaimClarityObj(cl,html_selected("glcar"+cl),document.getElementById("glcari"+cl).value,"user");
  //alert(callback);
  document.getElementById("allgclar"+cl).innerHTML=gclargetallobjs(cl,callback);
  //eval(callback);
}

function gclarselectionrem(cl,callback) {
  RemoveClaimClarityObj(cl,html_selected("glcar"+cl),"user");
  document.getElementById("allgclar"+cl).innerHTML=gclargetallobjs(cl,callback);

  //eval(callback);
}

function gclaronselect(cl) {
  document.getElementById("glcari"+cl).value=GetClaimClarityObj(cl,html_selected("glcar"+cl));
}

function gclarselection(cl,pref,callback) {
  var html="";
  if(!pref) {
    pref=CLAR_OBJS[0];
  }
  if(CLAR_OBJS.length>0) {
    html="<table width=100% class=SMALLTAB><TH>C "+html_selection("glcar"+cl,CLAR_OBJS,pref,"gclaronselect("+cl+")");
    html+="<input type=text  class=GCLAR size=2 id='glcari"+cl+"' value='"+GetClaimClarityObj(cl,pref)+"'>";
    html+="<button class=GCLAR onclick='gclarselectionadd("+cl+",\""+(callback||"")+"\")'>"+html_img(smalladd_img)+"</button></TH> ";
  }
  
  html+="<TD id=allgclar"+cl+" align=right>"+gclargetallobjs(cl)+"</TD></TABLE>";
  return html;
}



function Normalize_Clarity(word) {
  var cmd=word.substring(0,4).toUpperCase();  
  var txtout="";
  switch(cmd) {        
  case "/UNC":
    txtout="/UNCLEAR";
    break;
  case "/SUP":
    txtout="/SUPPORT";
    break;
  case "/BRO":
    txtout="/BROAD";
    break;
  case "/RES":
    txtout="/RESULT";
    break;
  }
  return txtout;
}

function clar_doconciseness(clist){
  var cl_iter=new List_iterator(clist);

  // concise is a special case
  var rule29=[];
  var r29idx,r29;
  var clt_iter=new cltype_iterator();
  while(!clt_iter.end()) { // group claims in same categories
    rule29[clt_iter.getValue()]= new List();
    clt_iter.next();
  }
  while(!cl_iter.end()) {
    var cl=cl_iter.getValue();
    r29=GetClaimCategory(cl);      
    // group independent claims in cats to check conciseness
    if(claim_isgrpindependent(cl) && kw_isCatMaxOne(r29)) { // r29 !="E" && r29 !="O"
      RemoveClaimClarityObj(cl,"concise","auto");
      rule29[r29].push(cl);
    }
    cl_iter.next();
  }
  // build lack of concise objection
  var objrule29=new List();
  var clt_iter=new cltype_iterator();
  while(!clt_iter.end()) { // group claims in same categories
    r29=clt_iter.getValue();
    if(r29 !="E" && r29 !="O" && rule29[r29].length() >1) {
      var cl_iter=rule29[r29].iterator();
      var param = cl_iter.getValue();
      while(!cl_iter.end()) {
	AddClaimClarityObj(cl_iter.getValue(),"concise",param,"auto");
	cl_iter.next();
      }
    }
    clt_iter.next();
  }   
}  


function xxx_doclarity(clist,law) {
  var html="";
  var cl_iter=new List_iterator(clist);

  // new XML support
  var clarxmlobjs =new Array();
  var clarxml_iter= new StdClause_Iterator("@type=\"clarity\" && @active=\"yes\"");
  while(!clarxml_iter.end()) {
    clarxmlobjs[clarxml_iter.label()]= new objectiondict();
    clarxml_iter.next();
  }

  
  // one entry per type of clarity objection
  var clarobjs=new Array();
  var gclarobjs=new Array();
  var gclarfound=[];
  var claridx,clar;

  for(claridx in CLAR_HINTS) {
    clarobjs[CLAR_HINTS[claridx]]= new List();
  }

  for(claridx in CLAR_OBJS) {
    gclarobjs[CLAR_OBJS[claridx]]= new objectiondict();
  }

  var art84   =clarobjs[""]; // just for readability

  // concise is a special case
  clar_doconciseness(clist);
  
  // Collect objections in technical features
  // scan independent claims for lack of conciseness
  while(!cl_iter.end()) {
    var cl=cl_iter.getValue();   
    if(IsClaimUnclear(cl)) {
      art84.push(cl);
      // check  for global clarity objections
      var objs_iter= new claimclarity_iterator(cl);
      while(!objs_iter.end()) {
	gclarfound[objs_iter.getType()]=1;
	gclarobjs[objs_iter.getType()].add(cl,objs_iter.getParam());
	objs_iter.next();
      }
      // check tech features for clarity objections
      var tf_iter= new claim_features_iterator(cl);
      while(!tf_iter.end()) {
	var clar=GetFeatureUnclarityEq(cl,tf_iter.getValue());
	if(clar) {
	  if(clarxmlobjs[clar]) { // XML support
	    if(tf_iter.getEq()) { // if relates to eq, use eq
	      var eqtf=new claim_tf(tf_iter.getEq());
	      clarxmlobjs[clar].add(cl,partrim(GetFeatureDesc(eqtf.getcl(),eqtf.gettf())));
	    } else {
	      clarxmlobjs[clar].add(cl,partrim(tf_iter.getDesc()));
	    }
	  } 
	}
	tf_iter.next();
      }
    }
    cl_iter.next();
  }

  // handle Art123
  if(clarxmlobjs["added"].length()) { 
    html+=stdcls_apply_clar("added",clarxmlobjs["added"],law)+BR2;       
  }   



  // handle other clarity objections
  if(art84.length()) { 
    html+=stdcls_apply_clar("header",art84,law)+BR2;

    for(var claridx in  CLAR_HINTS) {
      clar=CLAR_HINTS[claridx];
      switch(clar) {
      case "added":
      case "":
	// already done
	break;
      default:
	if(clarxmlobjs[clar]) {
	  if(clarxmlobjs[clar].length()) {
	    html+=stdcls_apply_clar(clar,clarxmlobjs[clar],law)+BR2;
	  }
	}
      }
    }  
  }

  for(var claridx in  gclarfound) { 
    html+=stdcls_apply_clar(claridx,gclarobjs[claridx],law)+BR2;
  }
  
  return html;
}


function epc_doclarity(clist) {
  return xxx_doclarity(clist,"epc");
}

function pct_doclarity(clist) {
  return xxx_doclarity(clist,"pct"); 
}

